package com.yc.springcloud.controller;

import com.yc.springcloud.entity.Member;
import com.yc.springcloud.service.MemberService;
import com.yc.springcloud.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author DELL
 * @Date 2024/10/3 11:26
 * @Version 1.0
 */
@Slf4j
@Controller
public class MemberController {

	@Resource
	private MemberService memberService;

	//1. 我们的前端如果是以json格式来发送添加信息Member， 那么我们需要使用@RequestBody
	//   , 才能将数据封装到对应的bean, 同时保证http的请求头的 content-type是对应
	//2. 如果前端是以表单形式提交了，则不需要使用@RequestBody, 才会进行对象参数封装, 同时保证
	//   http的请求头的 content-type是对应

	@ResponseBody
	@GetMapping(value = "/member/get/{id}")
	public Result queryMemberById(@PathVariable("id")  Long id){

		Member member = memberService.queryMemberById(id);
		if (member == null){
			return Result.error("402-10002",id + "不存在" );
		}else{
			return Result.success("查找成功-10002",member);
		}
	}

	@ResponseBody
	@PostMapping(value = "/member/saveMember")
	public Result save(@RequestBody Member member){
		int affected = memberService.saveMember(member);
		log.info("reset= " + affected);
		if (affected > 0) { //成功
			return Result.success("添加用户成功-10002", affected);
		} else {
			return Result.error("401-10002", "添加用户失败");
		}
	}
}
